package com.gz;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;

//UDP没有明确的客户端和服务端，只有发送端和接收端
public class reserver {
    public static void main(String[] args) throws IOException {
        DatagramSocket datagramSocket = new DatagramSocket(9999);
        byte[] buf = new byte[1024];
        DatagramPacket packet = new DatagramPacket(buf, buf.length);

        System.out.println("服务器等待接收");
        datagramSocket.receive(packet);

        //接收
        int length = packet.getLength();
        String str = new String(packet.getData(), 0, length);
        System.out.println(str);


        //回复发送端
        buf = "好的发送端，我也去".getBytes();
        DatagramPacket datagramPacket =
                new DatagramPacket(buf, buf.length, InetAddress.getByName("192.168.119.1"), 9998);
        datagramSocket.send(datagramPacket);


        //关闭
        datagramSocket.close();
    }
}
